<?PHP


class media
{
	var $post;
	var $where;
	var $data;
	var $input;
	var $output;
	var $files;
	var $mode;
	var $resize_image;
	var $resize_thumb;
	var $raw_image;
	var $gallery_image;
	var $img_type;
	
	function get_media()
	{
			$this->data=get_sql('media','id',$where,NULL);
	}
	function process_gallery_images()
	{
		
	}
	
	function show_or_create($file,$x,$y)
	{
		
	}
	
	function resize_and_show($file,$x,$y)
	{
		
		global $conf;
			
				$s3 = new S3($conf->a['s3-access-key'],$conf->a['s3-secret']);
		
				
					$temp_file=$conf->a['photo-dir'].randomchar(10,true,false).$file;	
					if($object=$s3->getObject($conf->a['s3-image-bucket-name'],$file,$temp_file))
					{
								$file_mod = PhpThumbFactory::create($temp_file);

								$file_mod->adaptiveResize($x, $y);
								$file_mod->show();  

								unlink($temp_file);
					
					
					
					
					
					}

				
			



	}

	function process_image()
	{
		
		
		if($this->gallery_image)
		{
		
			for($x=1;$x<=10;$x++)
			{
			
				if(!empty($this->files['upload_image_'.$x]['tmp_name']))
				{
					
						$this->raw_image=trim($this->files['upload_image_'.$x]['tmp_name']);
						$this->img_type=$this->files['upload_image_'.$x]['type'];
						$file=$this->process_resize();
						$files[]=$file;
				}

			}
			
			return $files;
		}
		else
		{ 
		    
			$this->raw_image=trim($this->files[$this->input['raw']]['tmp_name']);
			$this->img_type=$this->files[$this->input['raw']]['type'];
			$file=$this->process_resize();
			
			return $file;
		}
		
	}
	function process_resize()
	{
			if(file_exists($this->raw_image))
			{
				
					rp($this->files);
					global $conf;
					if($this->img_type=='image/jpeg'){$extension='jpg';}
					else if($this->img_type=='image/png'){$extension='png';}
					else if($this->img_type=='image/jpg'){$extension='jpg';}
					$file['image']=randomchar(3,true,true).'-'.csq($this->post['title']).".".$extension;
					$file['thumb']=randomchar(3,true,true).'-'.csq($this->post['title']).".".$extension;
					copy($this->raw_image,$conf->a['thumb-dir'].$file['thumb']);

					mp('resizing');

					mp('copy');

					$thumb[1] = PhpThumbFactory::create($conf->a['thumb-dir'].$file['thumb']);
					if($this->resize_thumb)
					{
								printmessage('Resizing thumb to: '.$post['resize_x_thumb'].' x '.$post['resize_y_thumb']);
							$thumb[1]->adaptiveResize($this->input['resize_x_thumb'], $this->input['resize_y_thumb']);
					}
					else
					{
							$thumb[1]->adaptiveResize($conf->a['thumb-x'],$conf->a['thumb-y']);
					}
						$thumb[1]->save($conf->a['thumb-dir'].$file['thumb']);
						
						
						mp('saving');
						move_uploaded_file($this->raw_image,$conf->a['photo-dir'].$file['image']);
						
						
						
						if($this->resize_image)
						{
							mp('resizing image');
							$thumb[2]=PhpThumbFactory::create($conf->a['photo-dir'].$file['image']);
							$thumb[2]->adaptiveResize($this->input['resize_x'], $this->input['resize_y']);
							$thumb[2]->save($conf->a['photo-dir'].$file['image']);
						}

						mp('uploading to server');
						
						
						
						
						
						
						
						
						
						
						if($conf->a['s3-image-enabled']=='true')
						{
						//	echo "making s3 connection : ".$conf->a['s3-access-key']." - ".$conf->a['s3-secret']."    ";
							$s3 = new S3($conf->a['s3-access-key'],$conf->a['s3-secret']);
							//$s3
							// 
							// 
							// e
							//echo ' uploading : '.$file['image'].' and : '.$file['thumb'];
							
							
								$metaHeaders = array();
							   //move the file
							
								$source_file=$conf->a['photo-dir'].$file['image'];
							
							   if ($s3->putObjectFile($source_file, $conf->a['s3-image-bucket-name'], $file['image'], S3::ACL_PUBLIC_READ)) {
							    printmessage("S3: File '".$file['image']."' Uploaded");
							   }else{
							    printmessage("S3: File '".$file['image']."' Failed");
							   }
							
							
							
								$source_file=$conf->a['thumb-dir'].$file['thumb'];
							if ($s3->putObjectFile($source_file, $conf->a['s3-image-bucket-name'], $file['thumb'], S3::ACL_PUBLIC_READ)) {
							    printmessage("S3: File '".$file['thumb']."' Uploaded");
							   }else{
							    printmessage("S3: File '".$file['thumb']."' Failed");
							   }
							
							
							
							
							
						}
						//$conf->a['thumb-dir']
						//$conf->a['thumb-dir']
						//$conf->a['thumb-dir']
						
						
						
						return $file;
			}
			else
			{
				return FALSE;
			}
	}
	function get_gallery_images()
	{
		$where=process_input($this->where);
		$this->data=get_sql('media','id',$where,$this->options);
		
	}
	
	
	function add_image()
	{
		
	}
	
	function delete_image()
	{
		
	}
	
	function replace_image()
	{
		
		
	}
}
?>